import request from "@/utils/request";
import { WIDGET } from "@/components/flow/common/FlowConstant";

// 表单
function pageForms(data) {
  return request({
    url: "/api/formModel/page",
    method: "post",
    data,
  });
}

function listForms() {
  return request({
    url: "/api/formModel/list",
    method: "get",
  });
}

function queryForms(data) {
  return request({
    url: "/api/formModel/query",
    method: "post",
    data,
  });
}

function InsertOrUpdateForms(data) {
  const insertUrl = "/api/formModel/insert";
  const updateUrl = `/api/formModel/update/${data.id}`;
  return request({
    url: `${data.id ? updateUrl : insertUrl}`,
    method: "post",
    data,
  });
}

function getFormsById(id) {
  return request({
    url: `/api/formModel/get/${id}`,
    method: "get",
  });
}

function deleteFormById(id) {
  return request({
    url: `/api/formModel/remove/${id}`,
    method: "get",
  });
}

// 流程
function listGroups(data) {
  return request({
    url: "/api/flow/model/group/query",
    method: "post",
    data,
  });
}

function pageGroups(data) {
  return request({
    url: "/api/flow/model/group/page",
    method: "post",
    data,
  });
}

function InsertOrUpdateGroup(data) {
  const insertUrl = "/api/flow/model/group/insert";
  const updateUrl = `/api/flow/model/group/update/${data.id}`;
  return request({
    url: `${data.id ? updateUrl : insertUrl}`,
    method: "post",
    data,
  });
}

function getGroupById(id) {
  return request({
    url: `/api/flow/model/group/get/${id}`,
    method: "get",
  });
}

function deleteGroupById(id) {
  return request({
    url: `/api/flow/model/group/remove/${id}`,
    method: "get",
  });
}

function listGroupsWithFlowDefinition(params) {
  return request({
    url: "/flowdefinition/listGroupsWithFlowDefinition",
    method: "get",
    params,
  });
}

function listGroupsWithFlow(data) {
  return request({
    url: "/api/flow/model/group/detail/query",
    method: "post",
    data,
  });
}

function listGroupsWithEnabledFlowDefinition(params) {
  return request({
    url: "/flowdefinition/listGroupsWithEnabledFlowDefinition",
    method: "get",
    params,
  });
}

function saveOrUpdateGroup(data) {
  return request({
    url: "/api/flow/model/insert",
    method: "post",
    data,
  });
}

function deleteGroup(params) {
  return request({
    url: "/flowdefinition/deleteGroup",
    method: "get",
    params,
  });
}

function listFlowDefinitions(params) {
  return request({
    url: "/flowdefinition/listFlowDefinitions",
    method: "get",
    params,
  });
}

function getModelById(id) {
  return request({
    url: `/api/flow/model/get/${id}`,
    method: "get",
  });
}

function getFlowConfig(params) {
  return request({
    url: `/api/flow/model/get/${params.id}`,
    method: "get",
    params,
  }).then((resp) => {
    let flowDefJson = {};
    if (resp.code == 0) {
      flowDefJson = resp.data;
      return flowDefJson;
    }
  });
}

function saveOrUpdate(data) {
  return request({
    // url: "/flowdefinition/saveOrUpdate",
    url: data.id
      ? `/api/flow/model/update/${data.id}`
      : "/api/flow/model/insert",
    method: "post",
    data,
  });
}

function copyFlowModel(params) {
  return request({
    url: `/api/flow/model/clone`,
    method: "get",
    params,
  });
}

function removeById(params) {
  return request({
    // url: "/flowdefinition/removeById",
    url: `/api/flow/model/remove/${params.id}`,
    method: "get",
  });
}

function freezeById(data) {
  return request({
    url: "/api/flow/model/status/modify",
    method: "post",
    data,
  });
}

function enableById(params) {
  return request({
    url: "/flowdefinition/enableById",
    method: "get",
    params,
  });
}

function getFlowFormWidget(params) {
  return request({
    url: "/flowdefinition/getFlowFormWidget",
    method: "get",
    params,
  });
}

function deployFlowById(params) {
  return request({
    url: `/api/flow/model/deploy/${params.id}`,
    method: "get",
  });
}

// function getFlowForm(params) {
//   return request({
//     url: "/flowdefinition/getFlowForm",
//     method: "get",
//     params,
//   });
// }

function formWidgetListToMap(flowWidgets) {
  let flowWidgetMap = {};
  flowWidgets.forEach((flowWidget) => {
    let { name, type, details } = flowWidget;
    flowWidgetMap[name] = flowWidget;
    if (type == WIDGET.DETAIL)
      details.forEach((detail) => (flowWidgetMap[detail.name] = detail));
  });
  return flowWidgetMap;
}

export default {
  listForms,
  pageForms,
  queryForms,
  InsertOrUpdateForms,
  getFormsById,
  deleteFormById,
  listGroups,
  pageGroups,
  InsertOrUpdateGroup,
  getGroupById,
  deleteGroupById,
  listGroupsWithFlow,
  listGroupsWithFlowDefinition,
  listGroupsWithEnabledFlowDefinition,
  saveOrUpdateGroup,
  deleteGroup,
  listFlowDefinitions,
  getModelById,
  getFlowConfig,
  deployFlowById,
  removeById,
  saveOrUpdate,
  freezeById,
  enableById,
  getFlowFormWidget,
  formWidgetListToMap,
  copyFlowModel,
};
